<template>
	<jk-dialog
		v-model="showDialog"
		title="选择点位"
		width="900px"
		:append-to-body="true"
		:confirm-btn-loading="saveBtnLoading"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<div ref="box1" class="flex-end-center margin-bottom-4">
			<jk-search-input v-model="searchParams.signalName" placeholder="点位名称" class="search-bar-item-size2 item-gap" @on-enter="onSearchBtn"></jk-search-input>
			<jk-search-button @click="onSearchBtn"></jk-search-button>
		</div>
		<div class="data-list-container">
			<div class="data-list-container-inner" :style="{ height: `${dialogTableHeight - 30}px` }">
				<div
					v-for="(item, index) in singleList || []"
					:key="index"
					style="width: calc(100% / 6);padding: 2px;box-sizing: border-box;"
					@click="onSelectItem(item)"
				>
					<el-tag :effect="checkedArr && checkedArr.length ? checkedArr.map(x => x.id).includes(item.id) ? 'dark' : 'plain' : 'plain'" class="width-100-percent flex-between-center" :closable="false" type="primary">
						<span style="overflow: hidden;text-overflow: ellipsis;width: 100%;white-space: nowrap;">{{ item.name }}</span>
					</el-tag>
				</div>
				<span v-if="!singleList || !singleList.length" class="flex-center">暂无数据</span>
			</div>
		</div>
	</jk-dialog>
</template>
<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import {
        productionIotMachineGetSingleByProcessModel
    } from '../../../../api/basicData/machineModel';
    export default {
        components: {
        },
        mixins: [calHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            },
            formData: {
                type: Object,
                default() {
                    return {};
                }
            },
            checkedList: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        data() {
            return {
                checkedArr: [],
                singleList: [],
                tableLoading: false,
                saveBtnLoading: false,
                showDialog: false,
                saveDialogState: false,
                searchParams: {
                    signalName: ''
                }
            };
        },
        methods: {
            onSearchBtn() {
                this.getSingleListRequest();
            },
            onSelectItem(item) {
                const has = this.checkedArr.some(x => x.id === item.id);
                if (has) {
                    this.checkedArr = this.checkedArr.filter(x => x.id !== item.id);
                } else {
                    this.checkedArr.push(item);
                }
            },
            onConfirmBtn() {
                const arr = this.checkedArr ? this.checkedArr.map(x => {
                    return {
                        signalId: x.id,
                        signalName: x.name
                    };
                }) : [];
                this.$emit('on-confirm', arr);
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            getSingleListRequest() {
                if (this.formData.processCode) {
                    return productionIotMachineGetSingleByProcessModel({
                        machineModelId: this.formData.machineModelId,
                        processCode: this.formData.processCode,
                        signalName: this.searchParams.signalName
                    }).then(res => {
                        if (res.data.status === 200) {
                            this.singleList = res.data.res || [];
                        }
                    });
                }
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.checkedArr = this.checkedList ? this.checkedList.map(x => {
                        return {
                            id: x.singleId,
                            name: x.singleName
                        };
                    }) : [];
                    this.getSingleListRequest();
                }
            }
        }
    };
</script>
<style scoped lang="scss">
.data-list-container {
    background: #f8f8f9;
    padding: 10px;
    box-sizing: border-box;
    border-radius: 8px;
    .data-list-container-inner {
        overflow: auto;
        box-sizing: border-box;
        display: flex;
        flex-wrap: wrap;
        align-content: flex-start;
    }
}
</style>
